package com.wallellen.android.currencies;

import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Created by walle on 1/14/16.
 */
public class MainActivitiesTest extends ActivityInstrumentationTestCase2<MainActivity> {
    public MainActivitiesTest() {
        super(MainActivity.class);
    }

    private MainActivity mainActivity;
    private Button mCalcButton;
    private TextView mConvertedTextView;
    private EditText mAmountEditText;
    private Spinner mForSpinner;
    private Spinner mHomSpinner;

    @Override
    public void setUp() throws Exception {
        super.setUp();

        ArrayList<String> arrayList = new ArrayList(Arrays.asList(new String[]{"USD|United State Dollar", "EUR|Euro"}));
        Intent intent = new Intent();
        intent.putExtra(SplashActivity.KEY_ARRAYLIST, arrayList);
        setActivityIntent(intent);

        mainActivity = getActivity();
        mCalcButton = (Button) mainActivity.findViewById(R.id.btn_calc);
        mConvertedTextView = (TextView) mainActivity.findViewById(R.id.text_converted);
        mAmountEditText = (EditText) mainActivity.findViewById(R.id.edt_amount);
        mForSpinner = (Spinner) mainActivity.findViewById(R.id.spn_for);
        mHomSpinner = (Spinner) mainActivity.findViewById(R.id.spn_home);
    }

    public void proxyCurrencyConverterTask(final String str) throws Throwable {
        final CountDownLatch latch = new CountDownLatch(1);

        mainActivity.setCurrencyConverterTaskCallback(new MainActivity.CurrencyConverterTaskCallback() {
            @Override
            public void executionDone() {
                latch.countDown();
                Log.d(getName(), mConvertedTextView.getText().toString());
                Log.d(getName(), mAmountEditText.getText().toString());
                assertEquals(convertDouble(mConvertedTextView.getText().toString().substring(0, 5)), convertDouble(str));
            }

            private double convertDouble(String str) throws NumberFormatException {
                Log.d(getName(), "format number : " + str);
                return Double.parseDouble(str);
            }
        });

        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                mAmountEditText.setText(str);
                mForSpinner.setSelection(0);
                mHomSpinner.setSelection(1);
                mCalcButton.performClick();
            }
        });

        latch.await(30, TimeUnit.SECONDS);
    }

    public void testInteger() throws Throwable {
        proxyCurrencyConverterTask("12");
    }

    public void testFloat() throws Throwable {
        proxyCurrencyConverterTask("12.3");
    }
}
